package com.javabasic.algorithm.leetcode;

import java.util.Arrays;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * @since Created by work on 2021/12/23 11:10 下午
 */
public class MajorityElement {

    /**
     * 排序+个数
     * arr[i] == arr[i+count-1]
     * @param nums
     * @return
     */
    public int majorityElement(int[] nums) {
        int len = nums.length;
        Arrays.sort(nums);
        int count = Math.round(len*1.0F/2);
        for (int i = 0; i < len; i++) {
            if (nums[i] == nums[i + count-1]) {
                return nums[i];
            }
        }
        return -1;
    }
}
